package cn.com.headfree.commworkflow;

import cn.com.headfree.base.controller.ExBaseController;
import cn.com.headfree.base.controller.ResultData;
import cn.com.headfree.callback.call.CommRoleServerCall;
import cn.com.headfree.commworkflow.dto.ApproveWorkFlowDto;
import cn.com.headfree.commworkflow.dto.WorkFlowConditionJSONData;
import cn.com.headfree.commworkflow.model.TWorkFlowApprove;
import cn.com.headfree.commworkflow.model.TWorkFlowInfo;
import cn.com.headfree.commworkflow.model.TWorkFlowNodeApprove;
import cn.com.headfree.commworkflow.service.IWorkFlowApproveService;
import cn.com.headfree.commworkflow.service.IWorkFlowInfoService;
import com.gta.edu.sdk.common.constant.Constant;
import com.gta.edu.sdk.common.dto.Page;
import com.gta.edu.sdk.util.StringUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;


//@Controller
//@RequestMapping(value = "/commworkflow", produces = "application/json;charset=UTF-8")
public class CommWorkFlowControllerCopy extends ExBaseController{

	private static final Logger log = LogManager.getLogger(CommWorkFlowControllerCopy.class);

	@Autowired
	private IWorkFlowApproveService workFlowApproveService;
	@Autowired
	private IWorkFlowInfoService workFlowInfoService;

	/**
	 * 根据系统编码获取该系统下所有的工作流
	 * @param sysCode
	 * @return
	 */
	@RequestMapping(value="/findWorkFlowsBySysCode/{sysCode}",method=RequestMethod.GET)
	public ResultData findWorkFlowsBySysCode(String sysCode) {
		ResultData result = new ResultData();
		try {
			List<TWorkFlowInfo> flowList = this.workFlowInfoService.findWorkFlowsBySysCode(sysCode);
			result.setData(flowList);
		} catch(Exception e) {
			String errorMsg = "根据系统编码获取工作流列表出现了异常！";
			log.error(errorMsg,e);
			result.setSuccess(false);
			result.setErrorMsg(errorMsg);
		}
		return result;
	}

	/**
	 * 提交工作流
	 * @param workFlowId
	 * @param businessId 业务域id
	 * @param workFlowApproveCondition
	 * @param currentUserId
	 * @param approveAdvise 审批意见
	 * @param domainId
	 * @return
	 */
	@RequestMapping(value="/saveApproveRecord",method=RequestMethod.POST)
	public ResultData saveApproveRecord(String workFlowId,String businessId,String workFlowApproveCondition,String currentUserId,String approveAdvise,String domainId) {
		ResultData result = new ResultData();
		try {
			TWorkFlowApprove approve = new TWorkFlowApprove();
			approve.setWorkFlowId(workFlowId);
			approve.setBusinessId(businessId);
			if (!StringUtil.isNullOrEmpty(workFlowApproveCondition)) {
				approve.setApproveCondition(workFlowApproveCondition);
			}
			TWorkFlowApprove approveResult = workFlowApproveService.saveApproveRecord(approve,currentUserId,approveAdvise,domainId);
			result.setData(approveResult);
		} catch (Exception e) {
			String errorMsg = "审批工作流出现了异常!";
			log.error(errorMsg,e);
			result.setSuccess(false);
			result.setErrorMsg(errorMsg);
		}
		return result;
	}


	/**
	 * 审批流程
	 * @param approveId
	 * @param approveAdvise
	 * @param currentUserId
	 * @param domainId
	 * @param approveCondition
	 * @return
	 */
	@RequestMapping(value = "/approveRecord", method = RequestMethod.POST)
	public ResultData approveRecord(String approveId,String approveAdvise,String currentUserId,String domainId,String approveCondition) {
		ResultData result = new ResultData();
		try {
			TWorkFlowApprove approve = new TWorkFlowApprove();
			approve.setId(approveId);
			approve.setApproveCondition(approveCondition);
			if(this.workFlowApproveService.updateApproveRecord(approve,currentUserId,approveAdvise,domainId)){
				result.setData(1);
			} else {
				result.setData(0);
			}

		} catch(Exception e) {
			String errorMsg = "审批流程时出现了异常！";
			result.setSuccess(false);
			result.setErrorMsg(Constant.APPLY_UN_SUCCESS);
			log.error(errorMsg,e);
		}
		return result;
	}

	/**
	 * @description 分页查找系统编码下所有待审批任务
	 * @author fengya
	 * @date 2020年7月1日 上午11:26:54
	 * @param sysCode
	 * @param pageSize
	 * @param pageNo
	 * @return
	 * ResultData
	 */
	@RequestMapping(value = "/findWaitApproveWorkFlow/{sysCode}/{currentUserId}/{pageSize}/{pageNo}", method = RequestMethod.GET)
	public ResultData findWaitApproveWorkFlow(@PathVariable("sysCode") String sysCode, @PathVariable("currentUserId") String currentUserId, @PathVariable("pageSize") int pageSize, @PathVariable("pageNo") int pageNo) {
		ResultData result = new ResultData();
		try {
			Page<ApproveWorkFlowDto> page = new Page<ApproveWorkFlowDto>();
			page.setPageNo(pageNo);
			page.setPageSize(pageSize);
			this.workFlowApproveService.findWaitApproveWorkFlow(page, currentUserId, sysCode);
			result.setData(page);
		} catch (Exception e) {
			String errorMsg = "获取待审批任务出现了异常!";
			log.error(errorMsg,e);
			result.setSuccess(false);
			result.setErrorMsg(errorMsg);
		}
		return result;
	}

	/**
	 * @description 分页查找系统编码下所有已审批任务
	 * @author fengya
	 * @date 2020年7月1日 上午11:26:54
	 * @param sysCode
	 * @param pageSize
	 * @param pageNo
	 * @return
	 * ResultData
	 */
	@RequestMapping(value = "/findApprovedWorkFlow/{sysCode}/{currentUserId}/{pageSize}/{pageNo}", method = RequestMethod.GET)
	public ResultData findApprovedWorkFlow(String sysCode,String currentUserId,int pageSize,int pageNo) {
		ResultData result = new ResultData();
		try {
			Page<ApproveWorkFlowDto> page = new Page<ApproveWorkFlowDto>();
			page.setPageNo(pageNo);
			page.setPageSize(pageSize);
			this.workFlowApproveService.findApprovedWorkFlow(page, currentUserId, sysCode);
			result.setData(page);
		} catch (Exception e) {
			String errorMsg = "获取待审批任务出现了异常!";
			log.error(errorMsg,e);
			result.setSuccess(false);
			result.setErrorMsg(errorMsg);
		}
		return result;
	}

	/**
	 * @description 根据审批ID获取流程该节点审批条件数据
	 * 	 * @author fengya
	 * @date 2020年7月1日 下午3:59:56
	 * @param approveId
	 * @return
	 * ResultData
	 */
	@RequestMapping(value = "/getNextWorkFlowCondition/{approveId}", method = RequestMethod.GET)
	public ResultData getNextWorkFlowCondition(String approveId) {
		ResultData result = new ResultData();
		try {
			List<WorkFlowConditionJSONData> list = this.workFlowApproveService.getNextWorkFlowCondition(approveId);
			result.setData(list);
		} catch (Exception e) {
			String errorMsg = "获取待审批任务出现了异常!";
			log.error(errorMsg,e);
			result.setSuccess(false);
			result.setErrorMsg(errorMsg);
		}
		return result;
	}

	/**
	 * @description 根据业务ID获取该业务的审批记录
	 * @author fengya
	 * @date 2020年7月1日 下午7:35:21
	 * @param businessId
	 * @return
	 * ResultData
	 */
	@RequestMapping(value = "/findApprovedRecord/{businessId}", method = RequestMethod.GET)
	public ResultData findApprovedRecord(String businessId) {
		ResultData result = new ResultData();
		try {
			List<TWorkFlowNodeApprove> nodeList = this.workFlowApproveService.findApprovedRecord(businessId);
			result.setData(nodeList);
		} catch (Exception e) {
			String errorMsg = "获取待审批任务出现了异常!";
			log.error(errorMsg,e);
			result.setSuccess(false);
			result.setErrorMsg(errorMsg);
		}
		return result;
	}
}
